package cate.game.role.farm.endless;


import cate.common.table.d.GDEndless;
import cate.common.table.d.GDFight;
import cate.common.table.farm.endless.EndlessCombatRow;
import cp.solution.util.time.TimeSpanCheck;
import cate.game.role.farm.endless.msg.EndlessChallengeResultResp;
import cate.game.play.param.HpExtend;
import cate.game.play.proce.play.PlayPO;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.farm.AbstractFarmFTC;

public class EndlessFTC extends AbstractFarmFTC<MyEndlessPO> {

    /** 创建时间 */
    private long createTime;

    public EndlessFTC(Role role) {
        setup(role);
        this.createTime = System.currentTimeMillis();
        this.playStory.level = po(role).cur.combatNumTarget;
    }

    @Override
    protected MyEndlessPO po(Role role) {
        return role.getFarm().endless;
    }

    @Override
    public void handleResult(Role role, PlayPO play) {
        super.handleResult(role, play);
        boolean win = play.winner == GDFight.Team.A;
        boolean autoEnter; // 是否自动进入下一场战斗
        if (win && role.getGame().farm.local.endless.canPlay()) {
            if (TimeSpanCheck.spanDay(this.createTime) || !po(role).autoEnter) {
                autoEnter = false;
                role.sendNow(new EndlessChallengeResultResp(GDEndless.ChallengeResult.DAY_SPAN, role));
            } else {
                autoEnter = handleWin(role, play);
            }
        } else {
            // 挑战到此为止
            autoEnter = false;
            po(role).combatsOver();
            role.sendNow(new EndlessChallengeResultResp(GDEndless.ChallengeResult.DEAD, role));
        }
        po(role).noticeUpdate();
        if (autoEnter && po(role).autoEnter) {
            po(role).challenge(false);
        }
    }

    private boolean handleWin(Role role, PlayPO play) {
        MyEndlessPO po = po(role);
        EndlessCombatRow combatTpl = role.getGame().table.endless.combat.get(po.cur.combatNumTarget);
        if (combatTpl == null) {
            log.error("找不到无尽轮回关卡的配置id={}", po(role).cur.combatNumTarget);
            return false;
        }
        if (po.cur.combatNumTarget > po.rewardGotAt) {
            if (po.reward == null) {
                po.reward = new MixRes();
            }
            po.reward.addList(new MixRes(combatTpl.rewardStr));
            po.rewardGotAt = po.cur.combatNumTarget;
        }
        po.setCombatNumPassed(po.cur.combatNumTarget);
        po.cur.combatNumTarget += 1;
        po.cur.combatCount += 1;
        if (po.cur.combatCount > po.combatCountMax) {
            po.combatCountMax = po.cur.combatCount;
            role.getGame().rank.endless.record(role, po.combatNumStart + po.combatCountMax - 1L);
        }
        combatTpl = role.getGame().table.endless.combat.get(po.cur.combatNumTarget);
        if (combatTpl == null) {
            po.cur.setNeedChoose(false);
            po.combatsOver();
            role.sendNow(new EndlessChallengeResultResp(GDEndless.ChallengeResult.WIN_MAX, role));
            return false;
        }
        if (po.cur.hpExtend == null) {
            po.cur.hpExtend = new HpExtend();
        }
        po.cur.hpExtend.recordByUid(play.teamEnd.a);

        boolean autoEnter = po.cur.combatCount % GDEndless.CHOOSE_BUFF_GAP == 0 && role.getStatus().isOnline();
        if (autoEnter) {
            po.cur.setNeedChoose(true);
        } else {
            po.cur.setNeedChoose(false);
        }
        return !autoEnter;
    }
}
